package com.hiwhitley.himei.module.video;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.hiwhitley.himei.R;
import com.hiwhitley.himei.contract.CommonFindContract;
import com.hiwhitley.himei.contract.model.CommandFindModel;
import com.hiwhitley.himei.contract.presenter.CommonFindPresenter;
import com.hiwhitley.himei.entity.FindMoreInfo;
import com.hiwhitley.himei.mvpframe.base.BaseFrameFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import me.drakeet.multitype.MultiTypeAdapter;

/**
 * Created by hiwhitley on 2016/9/28.
 */

public class CommonFindFragment extends BaseFrameFragment<CommonFindPresenter, CommandFindModel> implements CommonFindContract.View {

    private static final String TAG = "CommonFindFragment";
    @BindView(R.id.rv_find)
    RecyclerView mRvFind;
    private String name;
    private int position;
    private static final String[] RANK = new String[]{"date", "shareCount"};
    private static final String TAG_NAME = "name";
    private static final String TAG_POSITION = "position";

    private MultiTypeAdapter mMultiTypeAdapter;
    private List<FindMoreInfo.ItemListEntity> mItemListEntities;


    public static CommonFindFragment newInstance(String name, int position) {

        CommonFindFragment fragment = new CommonFindFragment();
        Bundle args = new Bundle();
        args.putString(TAG_NAME, name);
        args.putInt(TAG_POSITION, position);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected int getContentViewId() {
        return R.layout.fragment_common_find;
    }

    @Override
    protected void init() {

        name = getArguments().getString(TAG_NAME);
        position = getArguments().getInt(TAG_POSITION, 0);

        String category = RANK[position];

        mPresenter.loadCommonFind(name, category);

    }

    @Override
    protected void setListener() {
        mRvFind.setLayoutManager(new LinearLayoutManager(mActivity));
        mItemListEntities = new ArrayList<>();

        mMultiTypeAdapter = new MultiTypeAdapter();
        mMultiTypeAdapter.register(FindMoreInfo.ItemListEntity.class, new CommonFindViewProvider());
        mRvFind.setAdapter(mMultiTypeAdapter);
    }


    @Override
    public void showCommonFind(List<FindMoreInfo.ItemListEntity> listEntities) {
        for (FindMoreInfo.ItemListEntity listEntity : listEntities) {
            if (!"video".equals(listEntity.getType()))
                continue;
            mItemListEntities.add(listEntity);
        }

        mMultiTypeAdapter.setItems(mItemListEntities);
        mMultiTypeAdapter.notifyDataSetChanged();
    }

    @Override
    public void onRequestStart() {

    }

    @Override
    public void onRequestEnd() {

    }
}
